<?php

/**
 * This is the model class for table "page".
 *
 * The followings are the available columns in table 'page':
 * @property integer $id
 * @property string $judul
 * @property string $deskripisi
 * @property string $isi
 * @property integer $page_kategori_id
 * @property integer $created_by
 * @property string $created_time
 * @property integer $updated_by
 * @property string $updated_time
 */
class Page extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Page the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'page';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('title, keyword, description, detail', 'required'),
			array('judul, deskripisi, isi', 'required'),
			array('judul', 'length', 'max'=>200),
			array('deskripisi, isi, page_kategori_id, created_by,created_time, updated_by, updated_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, judul, deskripisi, isi, page_kategori_id, created_by, created_time, updated_by, updated_time', 'safe', 'on'=>'search'),
		);
	}

	public function beforeSave(){
		$this->created_by = Yii::app()->user->getId();
		$this->created_time = date('Y-m-d H-i-s');
		$this->updated_by = Yii::app()->user->getId();
		$this->updated_time = date('Y-m-d H-i-s');

		return true;
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'judul' => 'Judul',
			'deskripisi' => 'Deskripisi',
			'isi' => 'Isi',
			'page_kategori_id' => 'Page Kategori',
			'created_by' => 'Created By',
			'created_time' => 'Created Time',
			'updated_by' => 'Updated By',
			'updated_time' => 'Updated Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($cat)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->condition = 'page_kategori_id='.$cat;
		$criteria->order='t.id DESC';

		$criteria->compare('id',$this->id);
		$criteria->compare('judul',$this->judul,true);
		$criteria->compare('deskripisi',$this->deskripisi,true);
		$criteria->compare('isi',$this->isi,true);
		$criteria->compare('page_kategori_id',$this->page_kategori_id);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('created_time',$this->created_time,true);
		$criteria->compare('updated_by',$this->updated_by);
		$criteria->compare('updated_time',$this->updated_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}